<?php

class Backend_AdminController extends Zend_Controller_Action
{

    protected $item_per_page = 20;
    protected $_redirector;

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('backend/layout');
        $this->view->title = "";
        $this->view->adminctrl = true;

        $this->_redirector = $this->_helper->getHelper("Redirector");

        // check session
        @session_start();
        if(!isset($_SESSION['admin'])){
            $this->_redirector->gotoUrl('/backend/index/login');
        }
        else{
            //role must be admin
            if($_SESSION['admin']['role_id']>2){
                $this->_redirector->gotoUrl('/backend/');
            }

            $this->view->session = $_SESSION['admin'];
        }
    }

    public function indexAction()
    {
        $this->view->title = "Admin Home";

        $page = $this->getRequest()->getParam("page",1);
        $offset = ($page-1) * $this->item_per_page;

        $admin_db = new Backend_Model_DbTable_Admin();
        $this->view->admins = $admin_db->getAllAdmins(array($offset,$this->item_per_page));

        //Delete or edit action
        if($this->getRequest()->isPost()){
            $post = $this->getRequest()->getPost();
            $act = $post['act'];
            
            if(!is_null($act)){
                if(strcmp($act,"delete") == 0){
                    $admin_db->deleteAdmin($post['id']);
                    $this->_redirector->gotoUrl('/backend/admin/');
                }
                else if(strcmp($act,"edit") == 0){
                    $this->_redirector->gotoUrl('/backend/admin/edit/id/'.$post['id']);
                }
            }
        }
    }

    public function addAction()
    {
        $this->_helper->layout->disableLayout();
        $this->view->title = "Add Admin";

        $form = new Backend_Form_AdminAdd();
        $this->view->form = $form;

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            //Validate input data
            if($form->isValid($postData)){
                try{
                    $postData['role_id'] = 2;

                    $admin_db = new Backend_Model_DbTable_Admin();
                    $admin_db->insertAdmin($postData);
                    $this->_redirector->gotoUrl('/backend/admin');
                } catch (Zend_Exception $ex){
                    $msg = $ex->getMessage();
                    if(strpos($msg, "1062")){
                        $msg = "The email already exists";
                    }

                    $error = array('message'=>$msg,'prev'=>'/backend/admin');
                    $_SESSION['error'] = $error;
                    $this->_redirector->gotoUrl('/backend/error');
                }
            }

            //Input is not valid
            else{
                $form->populate($postData);
            }
        }
    }

    public function editAction()
    {
        $this->_helper->layout->disableLayout();
        $this->view->title = "Edit Admin";

        $form = new Backend_Form_AdminEdit();
        $this->view->form = $form;

        $id = $this->getRequest()->getParam('id', 0);
        $this->view->id = $id;

        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();

            //Validate input data
            if ($form->isValid($postData)) {
                $admin_db = new Backend_Model_DbTable_Admin();
                $admin_db->updateAdmin($id, $postData);

                $this->_redirector->gotoUrl('/backend/admin/');
            } else {
                $form->populate($postData);
            }
        }
        else {
            if ($id > 0) {
                $admin_db = new Backend_Model_DbTable_Admin();
                $admin = $admin_db->getAdminById($id)->toArray();
                $form->populate($admin);
            }
            else{
                $this->_redirector->gotoUrl('/backend/admin');
            }
        }
    }

}





